package transactions;

import dinamica.*;

/**
 * Insert master record and an associated array of values (detail) 
 * <br><br>
 * Creation date: 5/03/2004<br>
 * Last Update: 5/03/2004<br>
 * (c) 2004 Martin Cordova<br>
 * This code is released under the LGPL license<br>
 * @author Martin Cordova (dinamica@martincordova.com)
 * */
public class InsertMaster extends GenericTableManager
{

	/* (non-Javadoc)
	 * @see dinamica.GenericTransaction#service(dinamica.Recordset)
	 */
	public int service(Recordset inputParams) throws Throwable
	{

		//execute all recordsets and queries defined in config.xml
		int rc = 0;
		super.service(inputParams);
				
		//read confir parameters
		String colNames = getConfig().getConfigValue("colname");
		String sqlFiles = getConfig().getConfigValue("sql-template");

		if(colNames != null) {           //update by Oasahi
    		String[] colNamesA = colNames.split(";");   //多个语句体用分号(;)分隔       "xxx1,yyy1;xxx2,yyy2"
    		String[] sqlFilesA = sqlFiles.split(";");   //SQL语句
            
//System.out.println("colNamesA.length ===" + colNamesA.length);
    		for (int i=0;i<colNamesA.length;i++)
    		{
                String sqlFile = sqlFilesA[i];

        		//pre-fill sql template with master record values
        		String sql = getSQL(getResource(sqlFile), inputParams);
        		
        		//insert detail records using reusable object
        		InsertDetail r = (InsertDetail)getObject("transactions.InsertDetail");
        		r.save(sql, colNamesA[i]);
    		}
    	}
		
		return rc;
		
	}

}
